package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.AdminLoginParam;
import com.xxxx.server.pojo.ReceiveChangePwdParams;
import com.xxxx.server.rest.RespBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Api("登录")
@RestController
public class LoginController {

	@Autowired
	private IAdminService adminService;
	@Autowired
	private IRoleService roleService;


	@ApiOperation("登录")
	@PostMapping("/login")
	public  RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {

		 return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
	}


	@ApiOperation("获取当前用户信息")
	@GetMapping("/admin/info")
	public  Admin getAdminInfo(Principal principal) {
		if (null == principal) {
			return null;
		}
		String username = principal.getName();
		Admin admin = adminService.getUserByUsername(username);
		admin.setPassword(null);
		//设置角色和权限
		admin.setRoles(adminService.getRoles(admin.getId()));
		return admin;
	}
	/**
	 * 返回值类型:respBean;入参admin
	 * @return
	 */
	@ApiOperation("修改当前用户信息")
	@PutMapping("/admin/info")
	public RespBean updateAdminInfo(@RequestBody Admin admin){
		return adminService.updateAdminInfo(admin);
	}

	/**
	 * 修改密码接口:
	 * 前端传过来json ——pass、checkPass、oldPass、adminId,封装一下!
	 * @param
	 * @param
	 * @param
	 */
	@ApiOperation("修改密码")
	@PutMapping("/admin/pass")
	public RespBean changePwd(@RequestBody ReceiveChangePwdParams params){
		return adminService.changePwd(params);
	}



	@ApiOperation("退出登录")
	@PostMapping("/logout")
	public RespBean logout() {
		return RespBean.success("注销成功！");
	}
}